import { defineStore } from 'pinia'
import { ref } from 'vue'
import { taskApi } from '@/api/task'
import type { Task } from '@/types'

export const useTaskStore = defineStore('task', () => {
  // 状态
  const tasks = ref<Task[]>([])
  const currentTask = ref<Task | null>(null)
  const loading = ref(false)

  // 获取所有任务
  const fetchTasks = async () => {
    loading.value = true
    try {
      const response = await taskApi.getAll()
      tasks.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据项目ID获取任务
  const fetchTasksByProject = async (projectId: number) => {
    loading.value = true
    try {
      const response = await taskApi.getByProject(projectId)
      tasks.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取分配给我的任务
  const fetchMyAssignedTasks = async () => {
    loading.value = true
    try {
      const response = await taskApi.getMyAssigned()
      tasks.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取我创建的任务
  const fetchMyCreatedTasks = async () => {
    loading.value = true
    try {
      const response = await taskApi.getMyCreated()
      tasks.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 根据状态获取任务
  const fetchTasksByStatus = async (status: string) => {
    loading.value = true
    try {
      const response = await taskApi.getByStatus(status)
      tasks.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取任务详情
  const fetchTask = async (id: number) => {
    loading.value = true
    try {
      const response = await taskApi.getById(id)
      currentTask.value = response
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 创建任务
  const createTask = async (taskData: Omit<Task, 'id' | 'createdAt' | 'updatedAt'>) => {
    loading.value = true
    try {
      const response = await taskApi.create(taskData)
      tasks.value.unshift(response)
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新任务
  const updateTask = async (id: number, taskData: Partial<Task>) => {
    loading.value = true
    try {
      const response = await taskApi.update(id, taskData)
      const index = tasks.value.findIndex(t => t.id === id)
      if (index !== -1) {
        tasks.value[index] = response
      }
      if (currentTask.value?.id === id) {
        currentTask.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 删除任务
  const deleteTask = async (id: number) => {
    loading.value = true
    try {
      await taskApi.delete(id)
      tasks.value = tasks.value.filter(t => t.id !== id)
      if (currentTask.value?.id === id) {
        currentTask.value = null
      }
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新任务状态
  const updateTaskStatus = async (id: number, status: string) => {
    loading.value = true
    try {
      const response = await taskApi.updateStatus(id, status)
      const index = tasks.value.findIndex(t => t.id === id)
      if (index !== -1) {
        tasks.value[index] = response
      }
      if (currentTask.value?.id === id) {
        currentTask.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 分配任务
  const assignTask = async (id: number, assigneeId: number) => {
    loading.value = true
    try {
      const response = await taskApi.assign(id, assigneeId)
      const index = tasks.value.findIndex(t => t.id === id)
      if (index !== -1) {
        tasks.value[index] = response
      }
      if (currentTask.value?.id === id) {
        currentTask.value = response
      }
      return response
    } catch (error) {
      throw error
    } finally {
      loading.value = false
    }
  }

  // 清空当前任务
  const clearCurrentTask = () => {
    currentTask.value = null
  }

  return {
    // 状态
    tasks,
    currentTask,
    loading,
    // 方法
    fetchTasks,
    fetchTasksByProject,
    fetchMyAssignedTasks,
    fetchMyCreatedTasks,
    fetchTasksByStatus,
    fetchTask,
    createTask,
    updateTask,
    deleteTask,
    updateTaskStatus,
    assignTask,
    clearCurrentTask
  }
})